#include "pwm.h"
#include "main.h"


#define PWM_IOCTL_SET_FREQ 1
#define PWM_IOCTL_STOP 0



PWM::PWM(QObject *parent) :
    QObject(parent)
{

   pwm_fd = ::open("/dev/pwm", O_RDONLY);
    ::ioctl(pwm_fd, PWM_IOCTL_STOP);

   timer=new QTimer();
   QObject:: connect( timer, SIGNAL(timeout()), SLOT(timeDone()) );
   timer2=new QTimer();
   QObject:: connect( timer2, SIGNAL(timeout()), SLOT(timeDone2()) );
   timer3=new QTimer();
   QObject:: connect( timer3, SIGNAL(timeout()), SLOT(timeDone3()) );

   //  tweet=0;

}
PWM::~PWM()
{
  delete timer;
  delete timer2;
  delete timer3;
   close(pwm_fd);
}


void PWM::pwm_work_on(int freq)
{
   ::ioctl(pwm_fd, PWM_IOCTL_SET_FREQ, freq);
}


void PWM::pwm_stop()
{
   ::ioctl(pwm_fd, PWM_IOCTL_STOP);
}

void PWM::pwm_tweet_till(int s) //急促报警
{
   ::ioctl(pwm_fd, PWM_IOCTL_SET_FREQ, 1000);
 //   tweet=1;
 timer2->start(100);
 timer3->setSingleShot(true);
 timer3->start(1000*s);

}


void PWM::pwm_tweet(int msec)
{
  ::ioctl(pwm_fd, PWM_IOCTL_SET_FREQ, 1000);
  timer->start(msec);
  timer->setSingleShot(true);
}



void PWM::timeDone()
{
  ::ioctl(pwm_fd, PWM_IOCTL_STOP);
}

void PWM::timeDone2()
{
        if(tweet) {::ioctl(pwm_fd, PWM_IOCTL_STOP);  tweet=0;}
       else   { ::ioctl(pwm_fd, PWM_IOCTL_SET_FREQ, 1000);  tweet=1;}

}

void PWM::timeDone3()
{
   timer2->stop();
    ::ioctl(pwm_fd, PWM_IOCTL_STOP);
}
